<?php
namespace app\chat\controller;


use app\chat\model\Message;
use think\Model;
use \think\Request;
use app\chat\controller\Base;
use app\chat\model\Order as OrderModel;
use app\chat\model\ChatUser as ChatUserModel;
use app\chat\server\Chat as ChatServer;

class index extends Base
{
    protected $beforeActionList = [
        'isLogin',
    ];

    // 渲染聊天界面
    public function chat(){
        if (getrole() == EMPLOYER){ // 雇主
            // 雇主 渲染的是最左侧的聊天
            $conversation = ChatServer::getConversation() ;
            $conversationone = [
                'name' =>'',
            ];
            if (!empty($conversation)){
                $conversationone =  ChatServer::getConversation()[0];
            }
            $this->assign([
                'conversationone' =>$conversationone,
                'conversation'    =>  $conversation // 获取历史会话 | 渲染左边第一列
            ]);
        }else if (getrole() == ADMIN){ // 管理员
            $conversation = ChatServer::getConversation() ;
            $conversationone = [
                'name' =>'',
            ];
            if (!empty($conversation)){
                $conversationone =  ChatServer::getConversation()[0];
            }
            $this->assign([
                'conversationone' =>$conversationone,
                'conversation'    =>  $conversation// 获取历史会话 | 渲染左边第一列
            ]);

            // 获取管理员的好友
            $Goodfriendlist = ChatServer::getGoodfriendlist();
            $this->assign([
                'Goodfriendlist' =>$Goodfriendlist,
            ]);
        }else if (getrole() == HOUSEKEEPER) {  // 管家
            $conversation = ChatServer::getConversation() ;
            $conversationone = [
                'name' =>'',
            ];
            if (!empty($conversation)){
                $conversationone =  ChatServer::getConversation()[0];
            }
            $this->assign([
                'conversationone' =>$conversationone,
                'conversation'    =>  $conversation// 获取历史会话 | 渲染左边第一列
            ]);

            // 获取管理员的好友
            $Goodfriendlist = ChatServer::getGoodfriendlist();
            $this->assign([
                'Goodfriendlist' =>$Goodfriendlist,
            ]);
        }else if (getrole() == DEVELOPER){ // 开发
            $conversation = ChatServer::getConversation() ;
            $conversationone = [
                'name' =>'',
            ];
            if (!empty($conversation)){
                $conversationone =  ChatServer::getConversation()[0];
            }
            $this->assign([
                'conversationone' =>$conversationone,
                'conversation'    =>  $conversation// 获取历史会话 | 渲染左边第一列
            ]);

            // 获取管理员的好友
            $Goodfriendlist = ChatServer::getGoodfriendlist();
            $this->assign([
                'Goodfriendlist' =>$Goodfriendlist,
            ]);
        }
        return $this->fetch();
    }

    // 发送消息
    public function sedMsg(Request $request){
        if($request->isAjax()){
            return json( ChatServer::sedMsg($request->post()));
        }
        return json([]);
    }

    // 获取聊天内容
    public function getMsgList(Request $request){
        if($request->isAjax()){
            return json( ChatServer::getMsg($request->post()));
        }
        return json([]);
    }
}
